home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSTHREAD.C < prev    next >
C/C++ Source or Header  |  1993-08-26  |  5KB  |  162 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsthread.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains for OS/2 threads.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <bs.h>
  45.  
  46. //----------------------------------------------------------------------------
  47. //    Globals
  48. //----------------------------------------------------------------------------
  49. #define DEFAULT_STACK_SIZE        (32 _K)
  50.  
  51.  
  52. //----------------------------------------------------------------------------
  53. //   Description: Terminate thread. This function becomes a macro on 
  54. //                          operating systems without threads.
  55. //    Parameters:    none
  56. //       Returns:    void
  57. //----------------------------------------------------------------------------
  58. VOID FN_E __ThreadEnd(void)
  59. {
  60.     Log(LOG_HIGH, "Thread %ld terminated.", (LONG)THREADID);
  61. #if (OS_OS2 || OS_PM) && COMPILE_MT
  62.     _endthread();
  63. #endif
  64.     return ;
  65. }
  66.  
  67.  
  68. //----------------------------------------------------------------------------
  69. //   Description:    Get thread id.
  70. //    Parameters:    none
  71. //       Returns:    Thread ID. The ID of the main thread is always 1.
  72. //----------------------------------------------------------------------------
  73. TID FN_E ThreadId(void)
  74. {
  75.     return THREADID;
  76. }
  77.  
  78.  
  79. //----------------------------------------------------------------------------
  80. //   Description:    Start a thread
  81. //    Parameters:    pfn        Thread function
  82. //                        ptid        Pointer to variable to receive thread id.
  83. //                                    If null, thread id is not returned.
  84. //                        pv            Argument to thread function.
  85. //       Returns:    TRUE if successful.
  86. //----------------------------------------------------------------------------
  87. BOOL FN_E ThreadStart(PFNTHRD pfn, PTID ptid, PVOID pv)
  88. {
  89. #if (OS_OS2 || OS_PM) && COMPILE_MT
  90.  
  91.     int threadid = _beginthread(pfn, DEFAULT_STACK_SIZE, pv);
  92.     if (threadid <= 0)
  93.         {
  94.         Halt("Unable to start thread");
  95.         return FALSE;
  96.         }
  97.  
  98.     Log(LOG_HIGH, "Thread %ld started", (LONG)THREADID);
  99.  
  100.     if (ptid)
  101.         *ptid = (TID)threadid;
  102.  
  103.     return TRUE;
  104.  
  105. #else
  106.  
  107.     (*pfn)(pv);                                    // Fake a thread!
  108.     if (ptid)
  109.         *ptid = (TID)1;
  110.  
  111.     return TRUE;
  112.  
  113. #endif
  114. }
  115.  
  116.  
  117. //----------------------------------------------------------------------------
  118. //   Description:    This is a thread function for the test function.
  119. //    Parameters:    pv        Pointer to string to print
  120. //
  121. //       Returns:    
  122. //----------------------------------------------------------------------------
  123. #if COMPILE_TEST
  124. static BOOL fContinue = FALSE;
  125.  
  126. VOID FN_T TestThreadFunction(PVOID pv)
  127. {
  128.     Output("Thread %d = %s\n", THREADID, pv);
  129.     fContinue = TRUE;
  130.     return ;
  131. }
  132. #endif
  133.  
  134.  
  135. //----------------------------------------------------------------------------
  136. //   Description:    Run standard test suite
  137. //    Parameters:    sTest        Test to run.
  138. //                                        0        Run all default tests (except).
  139. //       Returns:    TRUE if successful.
  140. //----------------------------------------------------------------------------
  141. #if COMPILE_TEST
  142. BOOL FN ThreadTest(SHORT sTest)
  143. {
  144.     TID tid;
  145.  
  146.     NOTUSED(sTest);
  147.     Output("Current thread id is %d\n", THREADID);
  148.  
  149.     if (!ThreadStart(TestThreadFunction, &tid, "This is a test"))
  150.         return FALSE;
  151.  
  152.     Output("Started thread %d\n", tid);
  153.     while (!fContinue)                        // While not done, give up time slice
  154.         Sleep(0);
  155.  
  156.     return TRUE;
  157. }
  158. #endif
  159. //----------------------------------------------------------------------------
  160. //------------------------------- End of File --------------------------------
  161. //----------------------------------------------------------------------------
  162.